package com.anolesoft.yr.mbus.crm.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.crm.dto.CrmBaseDTO;
import com.anolesoft.yr.mbus.crm.dto.CrmBasePartPriceDTO;
import com.anolesoft.yr.mbus.crm.dto.CrmBasePartPriceQueryDTO;
import com.anolesoft.yr.mbus.crm.service.CrmBasePartPriceService;
import com.anolesoft.yr.mbus.crm.vo.CrmBasePartPriceQueryVO;
import com.anolesoft.yr.mbus.crm.vo.CrmBasePartPriceVO;
import com.anolesoft.yr.mbus.crm.vo.CrmBaseVO;
import com.anolesoft.yr.mbus.part.dto.PartModelDTO;
import com.anolesoft.yr.mbus.part.vo.PartModelVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/12 10:42
 */
@RestController
public class CrmBasePartPriceController {
    @Autowired
    private CrmBasePartPriceService crmBasePartPriceService;

    @PostMapping(value = "/api/crm/crmbasepartprice/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(CrmBasePartPriceVO crmBasePartPriceVO) {
        CrmBasePartPriceDTO crmBasePartPriceDTO = vo2dto(crmBasePartPriceVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, crmBasePartPriceService.create(crmBasePartPriceDTO));
    }

    @PutMapping(value = "/api/crm/crmbasepartprice/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(CrmBasePartPriceVO crmBasePartPriceVO) {
        CrmBasePartPriceDTO crmBasePartPriceDTO = vo2dto(crmBasePartPriceVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, crmBasePartPriceService.modify(crmBasePartPriceDTO));
    }

    @DeleteMapping(value = "/api/crm/crmbasepartprice/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, crmBasePartPriceService.remove(id));
    }

    @GetMapping(value = "/api/crm/crmbasepartprice/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<CrmBasePartPriceDTO> crmBasePartPriceDTO = this.crmBasePartPriceService.selectOneById(id);
        if (crmBasePartPriceDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.crmBasePartPriceDTO2VO(crmBasePartPriceDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }

    @GetMapping(value = "/api/crm/crmbasepartprice/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<CrmBasePartPriceVO> vos = crmBasePartPriceService.selectAll().stream().map(t -> CustomBeanUtils.crmBasePartPriceDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBasePartPriceVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbasepartprice/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
        PageData<List<CrmBasePartPriceDTO>> pageData = crmBasePartPriceService.selectAll(limit, curr);
        List<CrmBasePartPriceVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.crmBasePartPriceDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBasePartPriceVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbasepartprice/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(CrmBasePartPriceVO crmBasePartPriceVO) {
        CrmBasePartPriceDTO crmBasePartPriceDTO = vo2dto(crmBasePartPriceVO, false);
        List<CrmBasePartPriceVO> vos = crmBasePartPriceService.selectList(crmBasePartPriceDTO).stream().map(t -> CustomBeanUtils.crmBasePartPriceDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBasePartPriceVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbasepartprice/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(CrmBasePartPriceVO crmBasePartPriceVO) {
        CrmBasePartPriceDTO crmBasePartPriceDTO = vo2dto(crmBasePartPriceVO, false);
        Integer limit = crmBasePartPriceVO.getRows();
        Integer curr = crmBasePartPriceVO.getPage();
        PageData<List<CrmBasePartPriceDTO>> pageData = crmBasePartPriceService.selectList(crmBasePartPriceDTO, limit, curr);
        List<CrmBasePartPriceVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.crmBasePartPriceDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBasePartPriceVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/api/crm/crmbasepartprice/list/condition/union/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageConditonList(@RequestBody CrmBasePartPriceVO crmBasePartPriceVO) {
        CrmBasePartPriceDTO crmBasePartPriceDTO = vo2dto(crmBasePartPriceVO, false);
        CrmBaseVO crmBase = crmBasePartPriceVO.getCrmBase();
        PartModelVO partModelVO = crmBasePartPriceVO.getPartModel();
        if (crmBasePartPriceVO != null) {
            if (crmBase != null) {
                CrmBaseDTO crm = CrmBaseDTO.builder()
                        .crmCode(crmBase.getCrmCode())
                        .crmName(crmBase.getCrmName())
                        .crmOtherName(crmBase.getCrmOtherName())
                        .build();
                crmBasePartPriceDTO.setCrmBase(crm);
            }
            if (partModelVO != null) {
                PartModelDTO model = PartModelDTO.builder()
                        .partTypeUid(partModelVO.getPartTypeUid())
                        .partBrand(partModelVO.getPartBrand())
                        .partName(partModelVO.getPartName())
                        .partModel(partModelVO.getPartModel())
                        .partCode(partModelVO.getPartCode())
                        .partSpecification(partModelVO.getPartSpecification())
                        .build();
                crmBasePartPriceDTO.setPartModel(model);
            }
        }

        Integer limit = crmBasePartPriceVO.getRows();
        Integer curr = crmBasePartPriceVO.getPage();
        PageData<List<CrmBasePartPriceDTO>> pageData = crmBasePartPriceService.selectPageConditionList(crmBasePartPriceDTO, limit, curr);
        List<CrmBasePartPriceVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.crmBasePartPriceDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBasePartPriceVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/crm/crmbasepartpricequery/list/condition/union/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageConditonListForCrmBasePartPriceQuery(CrmBasePartPriceQueryVO crmBasePartPriceQueryVO) {
        CrmBasePartPriceQueryDTO crmBasePartPriceQueryDTO = vo2dto(crmBasePartPriceQueryVO);

        Integer limit = crmBasePartPriceQueryVO.getRows();
        Integer curr = crmBasePartPriceQueryVO.getPage();
        PageData<List<CrmBasePartPriceQueryDTO>> pageData = crmBasePartPriceService.selectPageConditionList(crmBasePartPriceQueryDTO, limit, curr);
        List<CrmBasePartPriceQueryVO> vos = pageData.getItems().stream().map(t -> CustomBeanUtils.crmBasePartPriceQueryDTO2VO(t)).collect(Collectors.toList());
        PageInfo<List<CrmBasePartPriceQueryVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

//    private CrmBasePartPriceVO dto2vo(CrmBasePartPriceDTO crmBasePartPriceDTO) {
//        return CrmBasePartPriceVO.builder()
//                .objuid(crmBasePartPriceDTO.getObjuid())
//                .offerPerson(crmBasePartPriceDTO.getOfferPerson())
//                .crmBaseUid(crmBasePartPriceDTO.getCrmBaseUid())
//                .partModelUid(crmBasePartPriceDTO.getPartModelUid())
//                .factoryCode(crmBasePartPriceDTO.getFactoryCode())
//                .remark(crmBasePartPriceDTO.getRemark())
//                .offerTime(crmBasePartPriceDTO.getOfferTime())
//                .offerPrice(crmBasePartPriceDTO.getOfferPrice())
//                .modifyUserUid(crmBasePartPriceDTO.getModifyUserUid())
//                .modifyTime(crmBasePartPriceDTO.getModifyTime())
//                .deleteFlag(crmBasePartPriceDTO.getDeleteFlag())
//                .dealPrice(crmBasePartPriceDTO.getDealPrice())
//                .createUserUid(crmBasePartPriceDTO.getCreateUserUid())
//                .createTime(crmBasePartPriceDTO.getCreateTime())
//                .build();
//    }

    private CrmBasePartPriceDTO vo2dto(CrmBasePartPriceVO crmBasePartPriceVO, boolean newId) {
        return CrmBasePartPriceDTO.builder()
                .objuid(newId ? CommonUtils.fastUUID() : crmBasePartPriceVO.getObjuid())
                .offerPerson(crmBasePartPriceVO.getOfferPerson())
                .crmBaseUid(crmBasePartPriceVO.getCrmBaseUid())
                .partModelUid(crmBasePartPriceVO.getPartModelUid())
                .factoryCode(crmBasePartPriceVO.getFactoryCode())
                .remark(crmBasePartPriceVO.getRemark())
                .offerTime(crmBasePartPriceVO.getOfferTime())
                .offerPrice(crmBasePartPriceVO.getOfferPrice())
                .modifyUserUid(SecurityUtils.getUserId())
                .modifyTime(crmBasePartPriceVO.getModifyTime())
                .deleteFlag(crmBasePartPriceVO.getDeleteFlag())
//                .dealPrice(crmBasePartPriceVO.getDealPrice())
                .createUserUid(SecurityUtils.getUserId())
                .createTime(crmBasePartPriceVO.getCreateTime())
                .build();
    }

    private CrmBasePartPriceQueryDTO vo2dto(CrmBasePartPriceQueryVO t){
        return CrmBasePartPriceQueryDTO.builder()
                .objuid(t.getObjuid())
                .contMainUid(t.getContMainUid())
                .preOrderUid(t.getPreOrderUid())
                .prodProductUid(t.getProdProductUid())
                .crmBaseUid(t.getCrmBaseUid())
                .partModelUid(t.getPartModelUid())
                .offerPerson(t.getOfferPerson())
                .offerTime(t.getOfferTime())
                .offerPrice(t.getOfferPrice())


                .bcrmCode(t.getBcrmCode())
                .bcrmName(t.getBcrmName())
                .bcrmOtherName(t.getBcrmOtherName())

                .cisTemp(t.getCisTemp())
                .ccontTypeUid(t.getCcontTypeUid())
                .csignDate(t.getCsignDate())
                .csignAddress(t.getCsignAddress())
                .ccontCode(t.getCcontCode())
                .ccustomerContractCode(t.getCcustomerContractCode())
                .cpayment(t.getCpayment())
                .ccontTitle(t.getCcontTitle())
                .c1Name(t.getC1Name())

                .dquotationCode(t.getDquotationCode())
                .dstate(t.getDstate())

                .eprodTypeUid(t.getEprodTypeUid())
                .eprodCode(t.getEprodCode())
                .eprodName(t.getEprodName())
                .e1typeCode(t.getE1typeCode())
                .e1typeName(t.getE1typeName())
                .e1typeNature(t.getE1typeNature())

                .fpartCode(t.getFpartCode())
                .fpartName(t.getFpartName())
                .fpartBrand(t.getFpartBrand())
                .fpartModel(t.getFpartModel())
                .fpartSpecification(t.getFpartSpecification())
                .fpartTypeUid(t.getFpartTypeUid())
                .fquantity(t.getFquantity())
                .f1typeCode(t.getF1typeCode())
                .f1typeName(t.getF1typeName())
                .build();
    }
}
